//
//  TTextMessageCell.m
//  UIKit
//
//  Created by kennethmiao on 2018/9/17.
//  Copyright © 2018年 kennethmiao. All rights reserved.
//

#import "TTextMessageCell.h"
#import "TFaceView.h"
#import "TFaceCell.h"
#import "THeader.h"
#import "TUIKit.h"
#import "THelper.h"

@implementation TTextMessageCellData
-(CGSize)calculationTransStr:(NSString *)fyStr {
    CGSize fySize = [fyStr boundingRectWithSize:CGSizeMake(TTextMessageCell_Text_Width_Max-10, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
    return fySize;
}
@end

@interface TTextMessageCell()
{
    TTextMessageCellData *_subData;
}
@end

@implementation TTextMessageCell

- (CGSize)getContainerSize:(TTextMessageCellData *)data
{
    _content.attributedText = [self formatMessageString:data.content];
    CGSize contentSize = [_content sizeThatFits:CGSizeMake(TTextMessageCell_Text_Width_Max, MAXFLOAT)];
    return CGSizeMake(contentSize.width + 2 * TTextMessageCell_Margin, contentSize.height + 2 * TTextMessageCell_Margin);
}

- (void)setupViews
{
    [super setupViews];
    _bubble = [[UIImageView alloc] init];
    [super.container addSubview:_bubble];
    
    _content = [[UILabel alloc] init];
    _content.font = [UIFont systemFontOfSize:15];
    _content.numberOfLines = 0;
    [_bubble addSubview:_content];
    
    ///
    _gdyFyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_gdyFyBtn setImage:[UIImage imageNamed:getImagename(@"gdy-翻译")] forState:0];
    [_gdyFyBtn addTarget:self action:@selector(clickSecFyBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_gdyFyBtn];
    
    ///
    _gdyFyBox = [[UIView alloc]init];
    _gdyFyBox.layer.cornerRadius = 5;
    _gdyFyBox.layer.masksToBounds = YES;
    _gdyFyBox.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:_gdyFyBox];
    ///
    _gdyFyShowL = [[UILabel alloc]init];
    _gdyFyShowL.textColor = RGB_COLOR(@"#323232", 1);
    _gdyFyShowL.font = [UIFont systemFontOfSize:13];
    _gdyFyShowL.numberOfLines = 0;
    [_gdyFyBox addSubview:_gdyFyShowL];
    _gdyFyBtn.hidden = _gdyFyBox.hidden = _gdyFyShowL.hidden = YES;
    
}
#pragma mark - 翻译按钮
-(void)clickSecFyBtn {
    /// 不要重复翻译
    if (![PublicObj checkNull:_subData.fyStr]) {
        return;
    }
    YBWeakSelf;
    _gdyFyBtn.userInteractionEnabled = NO;
    [GDYTranslateTool requestTranslateWithStr:_subData.content complete:^(BOOL finish, NSString *finishStr) {
        dispatch_async(dispatch_get_main_queue(), ^{
            _gdyFyBtn.userInteractionEnabled = YES;
        });
        if (weakSelf.transEvent) {
            weakSelf.transEvent(finishStr);
        }
    }];
}

- (void)setData:(TTextMessageCellData *)data;
{
    //set data
    [super setData:data];
    _subData = data;
    _content.attributedText = [self formatMessageString:data.content];
    //update layout
    _bubble.frame = super.container.bounds;
    _content.frame = CGRectMake(TTextMessageCell_Margin, TTextMessageCell_Margin, _bubble.frame.size.width - 2 * TTextMessageCell_Margin, _bubble.frame.size.height - 2 * TTextMessageCell_Margin);
    
    /// 翻译
    _gdyFyBtn.hidden = !data.transBtnShow;
    _gdyFyBox.hidden = _gdyFyShowL.hidden = !data.transBoxShow;
    _gdyFyShowL.text = data.fyStr;
    
    if(data.isSelf){
        _content.frame = CGRectMake(TTextMessageCell_Margin*0.7, TTextMessageCell_Margin, _bubble.frame.size.width - 2 * TTextMessageCell_Margin, _bubble.frame.size.height - 2 * TTextMessageCell_Margin);
        _bubble.image = [[UIImage imageNamed:@"chat_bg_my"] resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{30,20,30,20}") resizingMode:UIImageResizingModeStretch];
        /*
        _bubble.image = [[[[TUIKit sharedInstance] getConfig] getResourceFromCache:TUIKitResource(@"sender_text_normal")] resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{30,20,30,20}") resizingMode:UIImageResizingModeStretch];
        _bubble.highlightedImage = [[[[TUIKit sharedInstance] getConfig] getResourceFromCache:TUIKitResource(@"sender_text_pressed")] resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{30,20,30,20}") resizingMode:UIImageResizingModeStretch];
        */
        _content.textColor = [UIColor whiteColor];
        
        _gdyFyBtn.frame = _gdyFyBox.frame = _gdyFyShowL.frame = CGRectZero;
    }
    else{
        _content.frame = CGRectMake(TTextMessageCell_Margin*1.3, TTextMessageCell_Margin, _bubble.frame.size.width - 2 * TTextMessageCell_Margin, _bubble.frame.size.height - 2 * TTextMessageCell_Margin);
        _bubble.image = [[UIImage imageNamed:@"chat_bg_other"] resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{30,20,30,20}") resizingMode:UIImageResizingModeStretch];
        /*
        _bubble.image = [[[[TUIKit sharedInstance] getConfig] getResourceFromCache:TUIKitResource(@"receiver_text_normal")] resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{30,20,30,20}") resizingMode:UIImageResizingModeStretch];
        _bubble.highlightedImage = [[[[TUIKit sharedInstance] getConfig] getResourceFromCache:TUIKitResource(@"receiver_text_pressed")] resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{30,20,30,20}") resizingMode:UIImageResizingModeStretch];
        */
        _content.textColor = [UIColor blackColor];
    
        _gdyFyBtn.frame = _gdyFyBox.frame = _gdyFyShowL.frame = CGRectZero;
        if (data.transBtnShow == YES) {
            _gdyFyBtn.frame = CGRectMake(super.container.right+5, super.container.top+6, 46, 46*29/78);
        }
        if (data.transBoxShow == YES) {
            _gdyFyShowL.frame = CGRectMake(5, 5, data.fySize.width, data.fySize.height);
            _gdyFyBox.frame = CGRectMake(super.container.left+10, super.container.bottom+5, data.fySize.width+10, data.fySize.height+10);
        }
    }
}

- (NSAttributedString *)formatMessageString:(NSString *)text
{
    //先判断text是否存在
    if (text == nil || text.length == 0) {
        NSLog(@"TTextMessageCell formatMessageString failed , current text is nil");
        return [[NSMutableAttributedString alloc] initWithString:@""];
    }
    //1、创建一个可变的属性字符串
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
    
    if([[TUIKit sharedInstance] getConfig].faceGroups.count == 0){
        return attributeString;
    }
    
    //2、通过正则表达式来匹配字符串
    NSString *regex_emoji = @"\\[[a-zA-Z0-9\\/\\u4e00-\\u9fa5]+\\]"; //匹配表情
    
    NSError *error = nil;
    NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:regex_emoji options:NSRegularExpressionCaseInsensitive error:&error];
    if (!re) {
        NSLog(@"%@", [error localizedDescription]);
        return attributeString;
    }
    
    NSArray *resultArray = [re matchesInString:text options:0 range:NSMakeRange(0, text.length)];
    
    TFaceGroup *group = [[TUIKit sharedInstance] getConfig].faceGroups[0];
    
    //3、获取所有的表情以及位置
    //用来存放字典，字典中存储的是图片和图片对应的位置
    NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:resultArray.count];
    //根据匹配范围来用图片进行相应的替换
    for(NSTextCheckingResult *match in resultArray) {
        //获取数组元素中得到range
        NSRange range = [match range];
        //获取原字符串中对应的值
        NSString *subStr = [text substringWithRange:range];
        
        for (TFaceCellData *face in group.faces) {
            if ([face.name isEqualToString:subStr]) {
                //face[i][@"png"]就是我们要加载的图片
                //新建文字附件来存放我们的图片,iOS7才新加的对象
                NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
                //给附件添加图片
                NSString *imageName = face.path;
                if ([lagType isEqual:EN]) {
                    if ([imageName isEqual:@"[互粉]"] ||
                        [imageName isEqual:@"[关注]"] ||
                        [imageName isEqual:@"[粉]"] ||
                        [imageName isEqual:@"[给力]"]) {
                        imageName = [imageName stringByAppendingFormat:@"_en"];
                    }
                }
                textAttachment.image = [UIImage imageNamed:imageName];
                //textAttachment.image = [[[TUIKit sharedInstance] getConfig] getFaceFromCache:face.path];
                //调整一下图片的位置,如果你的图片偏上或者偏下，调整一下bounds的y值即可
                textAttachment.bounds = CGRectMake(0, -4, 20, 20);
                //把附件转换成可变字符串，用于替换掉源字符串中的表情文字
                NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
                //把图片和图片对应的位置存入字典中
                NSMutableDictionary *imageDic = [NSMutableDictionary dictionaryWithCapacity:2];
                [imageDic setObject:imageStr forKey:@"image"];
                [imageDic setObject:[NSValue valueWithRange:range] forKey:@"range"];
                //把字典存入数组中
                [imageArray addObject:imageDic];
                
            }
        }
    }
    
    //4、从后往前替换，否则会引起位置问题
    for (int i = (int)imageArray.count -1; i >= 0; i--) {
        NSRange range;
        [imageArray[i][@"range"] getValue:&range];
        //进行替换
        [attributeString replaceCharactersInRange:range withAttributedString:imageArray[i][@"image"]];
    }
    return attributeString;
}
@end
